Within a network processor several data frames are processed at the same time. Each data frame is processed by one processor/thread. Each processor/thread operates independently from all the other processors/threads. Thus, as the software (picocode) processes a data frame, the software has no knowledge of other frames which have been, are being, or will be processed. As data frames are processed, a thread may need access to a shared resource. This shared resource is shared among all threads. To allow a thread access to the resource without interference from other threads, semaphores are used.
A semaphore is a mechanism which allows a processor/thread to use a resource without interference from another processor/thread. A semaphore is used to ensure one and only one processor/thread has “ownership” of a given resource at any given time.
A network processor is a multi-processor environment with resources which can be accessed by all processors/threads. Thus, semaphores are an intricate part of network processors. As discussed above, network processors process data frames which belong to one or more data flows. To increase performance and eliminate the need to use general system resources, semaphore management in hardware is implemented.
When writing software, it is often desirable to access more than one resource within the scope of a software function. If updates to these resources are necessary, it is desirable to lock these resources. As software functions get more complex, it becomes necessary to lock multiple resources. As a specific example, consider tree management on a Network Processor. In this algorithm, picocode may need to lock the root of the tree chain that it is working on. In addition, picocode may also need to lock the leaf address which may be updated from multiple places. Implementing this algorithm requires the locking of two semaphores simultaneously.
Another example is when common software functions are used. Common software routines are software routines that can be accessed from any thread. Some common routines cannot be run on multiple threads at the same time, for example, common software routines used in queue management. Picocode may need to access the Free Queue, and also a general purpose queue at the same time and this would be done with two separate routines protected by two different semaphores to prevent the routines from running on multiple threads. One can see that the ability to lock multiple semaphores at once can provide a significant performance increase. Some common code routines require multiple semaphores to be locked at the same time in order to maximize execution performance. Having multiple semaphores per thread allows all the threads to execute these common code routines. If only one semaphore is available, one thread would have to be dedicated to running the common code routines that require multiple semaphores. Multiple semaphores provide finer granularity in locking resources and provide the ability for different combinations of locked down resources. With a single semaphore, groups of resources would have to be pre-defined for locking down together. With multiple semaphores, the software has more freedom in the ability to pick and choose the combinations of resources to be locked down, providing a very flexible and powerful solution.